home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 23 code / Multipane Dialogs Code / Sample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-18  |  6.3 KB  |  285 lines  |  [TEXT/MMCC]

  1. #include "Sample.h"
  2. #include "MPDialogs.h"
  3. #include "DD.h"
  4. #include "SampleProcs.h"
  5.  
  6. /* Sample application demonstrating the use of the multi-pane dialog code
  7.  * Largly borrowed from the Sample Drag application in the
  8.  *   Macintosh Drag and Drop package.
  9.  */
  10.  
  11. // Comment out to use default Action Procedures.
  12. #define CUSTOM_PROCS
  13.  
  14. // Define this to use a modal dialog
  15. #define MODAL
  16.  
  17. // Menu constants
  18. #define kAppleMenuID    128
  19. #define kAboutMenuItem    1
  20. #define kFileMenuID        129
  21. #define kPrefsItem        1
  22. #define kDisplayItem    2
  23. #define kCloseItem        4
  24. #define kQuitMenuItem    6
  25. #define kAboutDLOG        128
  26. #define kPrefDLOG        200
  27.  
  28. // Global variables
  29. Boolean            gQuitFlag;        // true when the app should exit the main event loop
  30. DialogPtr        gAbout = NULL;    // modeless About dialog box
  31.  
  32. MenuHandle        gAppleMenuHandle, gFileMenuHandle; // For our menus
  33. DialogPtr        prefDlog = NULL; // The multi-pane dialog
  34. Handle            thePrefs = NULL; // Storage of the data entered in the dialog
  35.  
  36. // UPPs for the custom Action Procedures
  37. #ifdef CUSTOM_PROCS
  38. ClickActionUPP    myClickAction = NULL;
  39. EditActionUPP    myEditAction = NULL;
  40. #endif
  41.  
  42. /***************************************************
  43.  *  Typical Mac toolbox routines
  44.  ***************************************************/
  45.  
  46. // CreateMenus makes menus the old-fashioned way
  47. void CreateMenus()
  48. {
  49.     // create Apple menu
  50.     gAppleMenuHandle = GetMenu(kAppleMenuID);
  51.     AddResMenu(gAppleMenuHandle, 'DRVR');
  52.     InsertMenu(gAppleMenuHandle, 0);
  53.  
  54.     // create File menu
  55.     gFileMenuHandle = GetMenu(kFileMenuID);
  56.     InsertMenu(gFileMenuHandle, 0);
  57.     
  58.     DrawMenuBar();
  59. }
  60.  
  61. // DoMenuCommand handles user menu selections
  62. void DoMenuCommand(long menuVal)
  63. {
  64.     short theItem, theMenu;
  65.     Str255 deskAccessoryName;
  66.     WindowPtr whichWindow;
  67.     
  68.     theItem = LoWord(menuVal);
  69.     theMenu = HiWord(menuVal);
  70.  
  71.     switch (theMenu) {
  72.  
  73.         case kAppleMenuID:
  74.             if (theItem == kAboutMenuItem) {
  75.                 if (gAbout) SelectWindow(gAbout);
  76.                 else gAbout = GetNewDialog(kAboutDLOG, NULL, (WindowPtr) -1);
  77.             } else {
  78.                 GetItem(gAppleMenuHandle, theItem, deskAccessoryName);
  79.                 (void) OpenDeskAcc(deskAccessoryName);
  80.             }
  81.             break;
  82.  
  83.         case kFileMenuID:
  84.  
  85.             switch (theItem) {
  86.                 case kPrefsItem:
  87.                     if (!prefDlog) {
  88.                         prefDlog = OpenMPDialog(kPrefDLOG, NULL,
  89. #ifdef CUSTOM_PROCS
  90.                             myClickAction, myEditAction, NULL, &thePrefs);
  91. #else
  92.                             NULL, NULL, NULL, &thePrefs);
  93. #endif
  94.                     }
  95.                     if (prefDlog) SetMenusBusy();
  96.                     break;
  97.                 case kDisplayItem:
  98.                     DialogDisplay(thePrefs);
  99.                     break;
  100.                 case kCloseItem:
  101.                     if (!(whichWindow = FrontWindow())) break;
  102.                     if (whichWindow == gAbout) {
  103.                         DisposeDialog(gAbout);
  104.                         gAbout = NULL;
  105.                     } else if (whichWindow == prefDlog) {
  106.                         CloseMPDialog(&prefDlog);
  107.                         if (!prefDlog) SetMenusIdle(); // Dialog has closed
  108.                     }
  109.                     break;
  110.                 case kQuitMenuItem:
  111.                     gQuitFlag = true;
  112.                     break;
  113.             }
  114.             break;
  115.  
  116.     }
  117.     HiliteMenu(0);      // unhilight menu title
  118. }
  119.  
  120. // Dim menus while movable modal dialog is displayed
  121. void SetMenusBusy()
  122. {
  123. #ifdef MODAL
  124.     DisableItem(gAppleMenuHandle, 0);
  125.     DisableItem(gFileMenuHandle, 0);
  126. #else
  127.     DisableItem(gFileMenuHandle, kPrefsItem);
  128. #endif
  129.     DrawMenuBar();
  130. }
  131.  
  132. // Display all menu options
  133. void SetMenusIdle()
  134. {
  135. #ifdef MODAL
  136.     EnableItem(gAppleMenuHandle, 0);
  137.     EnableItem(gFileMenuHandle, 0);
  138. #else
  139.     EnableItem(gFileMenuHandle, kPrefsItem);
  140. #endif
  141.     DrawMenuBar();
  142. }
  143.  
  144. /***************************************************
  145.  *  finally, the main program and event loop
  146.  ***************************************************/
  147.  
  148. void main()
  149. {
  150.     EventRecord    mainEventRec;
  151.     Boolean        eventFlag;
  152.     WindowPtr    whichWindow;
  153.     
  154.     // initialize the toolbox
  155.     InitGraf((Ptr) &qd.thePort);
  156.     InitFonts();
  157.     InitWindows();
  158.     InitMenus();
  159.     TEInit();
  160.     InitDialogs(0);
  161.     InitCursor();
  162.  
  163.     FlushEvents(everyEvent,0);
  164.     MaxApplZone();
  165.  
  166.     // initialize application global
  167.     gQuitFlag = false;
  168.     
  169.     // make the menus
  170.     CreateMenus();
  171.     
  172.     // initialize universal procedure pointers
  173. #ifdef CUSTOM_PROCS
  174.     myClickAction = NewClickActionProc(MyClickAction);
  175.     myEditAction = NewEditActionProc(MyEditAction);
  176. #endif
  177.  
  178.     // main event loop
  179.     while (!gQuitFlag) {
  180.  
  181.         eventFlag = WaitNextEvent(everyEvent, &mainEventRec, 60*60*60, nil);
  182.         
  183.         // Inline version of AdjustMenus
  184.         AbleDisItem(kFileMenuID, kDisplayItem, thePrefs != NULL);
  185.  
  186.         // Let multi-pane dialog have a go at it first
  187.         if (DoMPDialogEvent(&prefDlog, &mainEventRec)) {
  188.             if (!prefDlog) SetMenusIdle(); // Dialog has closed
  189.             continue;
  190.         }
  191.  
  192.         // Handle our About dialog box        
  193.         if (IsDialogEvent(&mainEventRec)) {
  194.             DialogPtr theDialog;
  195.             short itemHit;
  196.             
  197.             if (DialogSelect(&mainEventRec, &theDialog, &itemHit))
  198.                 continue;
  199.         }
  200.         
  201.         // The multi-pane dialog code didn't handle it, we have to.
  202.         switch(mainEventRec.what) {
  203.         
  204.             case mouseDown:
  205.                 switch (FindWindow(mainEventRec.where, &whichWindow)) {
  206.     
  207.                     case inSysWindow:  // desk accessory window
  208.                         SystemClick(&mainEventRec, whichWindow);
  209.                         break;
  210.                         
  211.                     case inMenuBar:
  212.                         AbleDisItem(kFileMenuID, kCloseItem, FrontWindow() != NULL);
  213.                         DoMenuCommand(MenuSelect(mainEventRec.where));
  214.                         break;
  215.                         
  216.                     case inContent:
  217. #ifdef MODAL
  218.                         if (!prefDlog) {
  219. #endif
  220.                         if (whichWindow != FrontWindow()) {
  221.                             SelectWindow(whichWindow);
  222.                         }
  223. #ifdef MODAL
  224.                         } else SysBeep(2);
  225. #endif
  226.                         break;
  227.                     
  228.                     case inGoAway:
  229. #ifdef MODAL
  230.                         if (!prefDlog) {
  231. #endif
  232.                         if (gAbout == whichWindow &&
  233.                                     TrackGoAway(whichWindow, mainEventRec.where)) {
  234.                             DisposeDialog(gAbout);
  235.                             gAbout = NULL;
  236.                         }
  237. #ifdef MODAL
  238.                         } else SysBeep(2);
  239. #endif
  240.                     
  241.                     case inDrag:
  242. #ifdef MODAL
  243.                         if (!prefDlog) {
  244. #endif
  245.                         DragWindow(whichWindow, mainEventRec.where, &qd.screenBits.bounds);
  246. #ifdef MODAL
  247.                         } else SysBeep(2);
  248. #endif
  249.                         break;
  250.  
  251.                     default:
  252.                         // This application really doesn't do very much.
  253.                         break;
  254.                 }
  255.                 break;
  256.  
  257.             case keyDown:
  258.             case autoKey:
  259.                 if (mainEventRec.modifiers & cmdKey) {
  260.                     long mk = MenuKey(mainEventRec.message & charCodeMask);
  261.                     if (mk) {
  262.                         DoMenuCommand(mk);
  263.                         break;
  264.                     }
  265.                 }
  266.                 break;
  267.                 
  268.             // a real app should handle all of these events.
  269.             // Since the code is in Inside Mac:Mac Toolbox Essentials,
  270.             // you really have no excuse for not supporting all the
  271.             // standard low-level events
  272.             // however, this is just a sample program, so I have an excuse
  273.             
  274.             case updateEvt:
  275.             case activateEvt:
  276.             case osEvt:
  277.             case diskEvt:
  278.             case mouseUp:
  279.                 break;
  280.         }
  281.     }
  282.     
  283.     // Good night
  284. }
  285.